#include "switchwidget.h"

SwitchWidget::SwitchWidget(QWidget *parent):MqttWidget(parent)
{
    mWidget = new SwitchControl(this);
    mWidget->show();
    gridLayout->addWidget(mWidget);
    connect(mWidget,&SwitchControl::toggled,this,[=](bool checked){
        QMQTT::Message message;
        message.setQos(mData.qos);
        message.setTopic(mData.topic);
        QByteArray data = QString::number(checked).toUtf8();
        message.setPayload(data);
        sendMessage(message);       //发送消息
    });
}

SwitchWidget::~SwitchWidget()
{

}

void SwitchWidget::getMessage(QMQTT::Message mesage)
{
    if(mesage.topic() != mData.topic)
        return ;
    bool result = QVariant(QString(mesage.payload())).toBool();
    mWidget->setToggle(result);
}


